<template>
  <el-row class="app-container">
    <el-row class="app-bar" type="flex" justify="end">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd" />
    </el-row>
    <el-table
      :key="tableHeight"
      v-loading="tableLoading"
      :max-height="tableHeight"
      border
      stripe
      highlight-current-row
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="100" />
      <el-table-column prop="name" :label="$t('name')" min-width="140" />
      <el-table-column prop="ip" label="Host" min-width="140" sortable>
        <template #default="scope">
          {{ scope.row.ip }}:{{ scope.row.port }}
        </template>
      </el-table-column>
      <el-table-column
        prop="owner"
        :label="$t('serverPage.sshKeyOwner')"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column label="OS" min-width="100" show-overflow-tooltip>
        <template #default="scope">
          <svg-icon
            v-if="scope.row.osInfo !== ''"
            :icon-class="getOSIcon(scope.row.osInfo)"
          />
          {{ getOS(scope.row.osInfo) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="osInfo"
        label="CPU/MEMORY"
        min-width="100"
        show-overflow-tooltip
      >
        <template #default="scope">
          {{ getOSDetail(scope.row.osInfo) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        :label="$t('description')"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        prop="state"
        :label="$t('state')"
        width="70"
        align="center"
      >
        <template #default="scope">
          <el-switch
            :value="scope.row.state === 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="(value) => onSwitchState(value, scope.$index)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="insertTime"
        :label="$t('insertTime')"
        width="135"
        align="center"
      />
      <el-table-column
        prop="updateTime"
        :label="$t('updateTime')"
        width="135"
        align="center"
      />
      <el-table-column
        prop="operation"
        :label="$t('op')"
        width="180"
        align="center"
        :fixed="$store.state.app.device === 'mobile' ? false : 'right'"
      >
        <template #default="scope">
          <el-button
            icon="el-icon-data-line"
            @click="handleMonitor(scope.row)"
          />
          <el-button
            icon="el-icon-document-copy"
            @click="handleCopy(scope.row)"
          />
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end" style="margin-top: 10px; width: 100%">
      <el-pagination
        hide-on-single-page
        :total="pagination.total"
        :page-size="pagination.rows"
        background
        layout="prev, pager, next"
        @current-change="handlePageChange"
      />
    </el-row>
    <el-dialog
      v-model="dialogVisible"
      :fullscreen="$store.state.app.device === 'mobile'"
      :title="$t('setting')"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        v-loading="formProps.loading"
        :rules="formRules"
        :model="formData"
        label-width="130px"
        :label-position="
          $store.state.app.device === 'desktop' ? 'right' : 'top'
        "
      >
        <el-form-item :label="$t('namespace')" prop="namespaceId">
          <el-radio-group v-model="formData.namespaceId">
            <el-radio :label="getNamespace()['id']">
              {{ $t('current') }}
            </el-radio>
            <el-radio :label="0">{{ $t('unlimited') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('name')" prop="name">
          <el-input v-model="formData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Host" prop="ip">
          <el-input v-model="formData.ip" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Port" prop="port">
          <el-input v-model.number="formData.port" autocomplete="off" />
        </el-form-item>
        <el-form-item :label="$t('serverPage.sshKeyOwner')" prop="owner">
          <el-input
            v-model="formData.owner"
            autocomplete="off"
            placeholder="root"
          />
        </el-form-item>
        <el-form-item :label="$t('serverPage.sshKeyPath')" prop="path">
          <el-row type="flex">
            <el-input
              v-model="formData.path"
              style="flex: 1"
              autocomplete="off"
              placeholder="/root/.ssh/id_rsa"
            />
            <el-button
              :icon="'el-icon-copy-document'"
              type="success"
              :loading="formProps.copyPubLoading"
              :disabled="formData.path === ''"
              @click="getPublicKey"
            >
              {{ $t('serverPage.copyPub') }}
            </el-button>
          </el-row>
        </el-form-item>
        <!-- <el-form-item :label="$t('serverPage.sshKeyPassword')" prop="password">
          <el-input
            v-model="formData.password"
            autocomplete="off"
            placeholder=""
          />
        </el-form-item> -->
        <el-form-item :label="$t('description')" prop="description">
          <el-input
            v-model="formData.description"
            type="textarea"
            :autosize="{ minRows: 2 }"
          />
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="text"
            @click="formProps.showAdvance = !formProps.showAdvance"
          >
            {{ $t('serverPage.advance') }}
          </el-button>
        </el-form-item>
        <el-form-item v-show="formProps.showAdvance" label="Jump host">
          <el-input v-model="formData.jumpIP" autocomplete="off" />
        </el-form-item>
        <el-form-item v-show="formProps.showAdvance" label="Jump port">
          <el-input v-model.number="formData.jumpPort" autocomplete="off" />
        </el-form-item>
        <el-form-item
          v-show="formProps.showAdvance"
          :label="$t('serverPage.sshKeyOwner')"
        >
          <el-input
            v-model="formData.jumpOwner"
            autocomplete="off"
            placeholder="root"
          />
        </el-form-item>
        <el-form-item
          v-show="formProps.showAdvance"
          :label="$t('serverPage.sshKeyPath')"
        >
          <el-input
            v-model="formData.jumpPath"
            autocomplete="off"
            placeholder="/root/.ssh/id_rsa"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="space-between">
          <el-button :loading="formProps.loading" type="success" @click="check">
            {{ $t('serverPage.testConnection') }}
          </el-button>
          <el-row>
            <el-button @click="dialogVisible = false">
              {{ $t('cancel') }}
            </el-button>
            <el-button
              :disabled="formProps.disabled"
              type="primary"
              @click="submit"
            >
              {{ $t('confirm') }}
            </el-button>
          </el-row>
        </el-row>
      </template>
    </el-dialog>
  </el-row>
</template>

<script lang="ts">
export default { name: 'ServerIndex' }
</script>
<script lang="ts" setup>
import type { ElForm } from 'element-plus'
import { getNamespace } from '@/utils/namespace'
import {
  ServerList,
  ServerTotal,
  ServerPublicKey,
  ServerAdd,
  ServerEdit,
  ServerCheck,
  ServerToggle,
  ServerData,
} from '@/api/server'
import getTableHeight from '@/composables/tableHeight'
import { ref } from 'vue'
import { copy, humanSize } from '@/utils'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const router = useRouter()
const dialogVisible = ref(false)
const { tableHeight } = getTableHeight()
const tableLoading = ref(false)
const tableData = ref<ServerList['datagram']['list']>([])
const pagination = ref({ page: 1, rows: 16, total: 0 })
const form = ref<InstanceType<typeof ElForm>>()
const tempFormData = {
  id: 0,
  namespaceId: getNamespace()['id'],
  name: '',
  ip: '',
  port: 22,
  owner: '',
  path: '/root/.ssh/id_rsa',
  password: '',
  jumpIP: '',
  jumpPort: 0,
  jumpOwner: '',
  jumpPath: '',
  jumpPassword: '',
  description: '',
}
const formData = ref(tempFormData)
const formProps = ref({
  loading: false,
  showAdvance: false,
  copyPubLoading: false,
  disabled: false,
})
const formRules = {
  namespaceId: [
    { required: true, message: 'Namespace required', trigger: 'blur' },
  ],
  name: [{ required: true, message: 'Name required', trigger: 'blur' }],
  ip: [{ required: true, message: 'IP required', trigger: 'blur' }],
  port: [
    {
      type: 'number',
      required: true,
      min: 0,
      max: 65535,
      message: '0 ~ 65535',
      trigger: 'blur',
    },
  ],
  owner: [
    {
      required: true,
      message: 'SSH-KEY owner required',
      trigger: 'blur',
    },
  ],
  path: [{ required: true, message: 'SSH-KEY path required', trigger: 'blur' }],
  description: [{ max: 255, message: 'Max 255 characters', trigger: 'blur' }],
}

getList()
getTotal()

function getList() {
  tableLoading.value = true
  new ServerList(pagination.value)
    .request()
    .then((response) => {
      tableData.value = response.data.list
    })
    .finally(() => {
      tableLoading.value = false
    })
}

function getTotal() {
  new ServerTotal().request().then((response) => {
    pagination.value.total = response.data.total
  })
}

function getPublicKey() {
  formProps.value.copyPubLoading = true
  new ServerPublicKey({ path: formData.value.path })
    .request()
    .then((response) => {
      copy(response.data.key)
      ElMessage.success(t('serverPage.copyPubTips'))
    })
    .finally(() => {
      formProps.value.copyPubLoading = false
    })
}

// 分页事件
function handlePageChange(val = 1) {
  pagination.value.page = val
  getList()
}

function handleAdd() {
  restoreFormData()
  dialogVisible.value = true
}

function handleEdit(data: ServerData['datagram']) {
  formData.value = Object.assign({}, data)
  dialogVisible.value = true
}

function handleCopy(data: ServerData['datagram']) {
  formData.value = Object.assign({}, data)
  formData.value.id = 0
  dialogVisible.value = true
}

function handleMonitor(data: ServerData['datagram']) {
  router.push({ path: '/server/agent', query: { serverId: data.id } })
}

function onSwitchState(value: boolean, index: number) {
  const data = tableData.value[index]
  if (value) {
    new ServerToggle({ id: data.id, state: value ? 1 : 0 })
      .request()
      .then(() => {
        ElMessage.success('Need to bind project again')
        tableData.value[index].state = value ? 1 : 0
      })
  } else {
    ElMessageBox.confirm(
      t('serverPage.removeServerTips', { serverName: data.name }),
      t('tips'),
      {
        confirmButtonText: t('confirm'),
        cancelButtonText: t('cancel'),
        type: 'warning',
      }
    )
      .then(() => {
        new ServerToggle({ id: data.id, state: value ? 1 : 0 })
          .request()
          .then(() => {
            tableData.value[index].state = value ? 1 : 0
          })
      })
      .catch(() => {
        ElMessage.info('Cancel')
      })
  }
}

function check() {
  form.value?.validate((valid) => {
    if (valid) {
      formProps.value.loading = true
      formProps.value.disabled = true
      new ServerCheck(formData.value)
        .request()
        .then(() => {
          ElMessage.success('Connected')
        })
        .finally(() => {
          formProps.value.loading = false
          formProps.value.disabled = false
        })
      return Promise.resolve(true)
    } else {
      return Promise.reject(false)
    }
  })
}

function submit() {
  form.value?.validate((valid) => {
    if (valid) {
      if (formData.value.id === 0) {
        add()
      } else {
        edit()
      }
      return Promise.resolve(true)
    } else {
      return Promise.reject(false)
    }
  })
}

function add() {
  formProps.value.disabled = true
  new ServerAdd(formData.value)
    .request()
    .then(() => {
      getList()
      getTotal()
      ElMessage.success('Success')
    })
    .finally(() => {
      formProps.value.disabled = dialogVisible.value = false
    })
}

function edit() {
  formProps.value.disabled = true
  new ServerEdit(formData.value)
    .request()
    .then(() => {
      getList()
      ElMessage.success('Success')
    })
    .finally(() => {
      formProps.value.disabled = dialogVisible.value = false
    })
}

function restoreFormData() {
  formData.value = { ...tempFormData }
}

function getOS(osInfo: string): string {
  if (osInfo === '') return ''
  return osInfo.split('|')[0]
}

function getOSIcon(osInfo: string): string {
  if (osInfo === '') return ''
  else if (osInfo.toLowerCase().includes('centos')) return 'centos'
  else if (osInfo.toLowerCase().includes('ubuntu')) return 'ubuntu'
  else return 'question-mark-blue'
}

function getOSDetail(osInfo: string): string {
  if (osInfo === '') return ''
  const osArr = osInfo.split('|')
  return osArr[1] + ' cores ' + humanSize(Number(osArr[2]) * 1024)
}
</script>
<style lang="scss" scoped>
@import '@/styles/mixin.scss';
.template-dialog {
  padding-right: 10px;
  height: 400px;
  overflow-y: auto;
  @include scrollBar();
}
</style>
